﻿using Infrastructure.Operator;
using Infrastructure.Web;
using MyApplication;
using MyData.Dto;
using MyData.Models;
using MyWeb.App_Start;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyWeb.Controllers
{
    [HandlerLogin]
    public class AdminUserController : BaseController
    {

        // GET: AdminUser
        [HandlerLogin]
        public ActionResult Index()
        {
            return View();
        }
        [HandlerLogin]
        public ActionResult AdminUserView(string id)
        {
            ViewBag.Departs = departApp.GetList();
            if (!String.IsNullOrEmpty(id))
            {
                var entity = adminUserApp.GetAdminUser(Guid.Parse(id));
                return View(entity);
            }
            return View(new AdminUserDto());
        }
        public ActionResult ConfigMenu(string Id)
        {
            if (!String.IsNullOrEmpty(Id))
            {
                var entity = adminUserApp.GetAdminUser(Guid.Parse(Id));
                return View(entity);
            }
            return View(new AdminUserDto());
        }
        public ActionResult EditPassWord()
        {
            return View();
        }
        public ActionResult GetAdminUser(QueryListModel query, string UserName,string NickName)
        {
            var result = adminUserApp.GetAdminUser(query, UserName, NickName);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult SaveAdminUser(string Id, string UserName, string PassWord,string NickName,Guid DepartId)
        {
            return base.Try(() =>
            {
                AdminUser model = new AdminUser();
                if (!String.IsNullOrEmpty(Id))
                    model.Id = Guid.Parse(Id);
                model.UserName = UserName;
                model.PassWord = PassWord;
                model.NickName = NickName;
                if (UserName == "admin")
                {
                    model.DepartId = null;
                }
                else
                {
                    model.DepartId = DepartId;
                }
                adminUserApp.Save(model);
            });
        }
        [HttpPost]
        public JsonResult AdminUserDelete(string Id)
        {
            return base.Try(() =>
            {
                adminUserApp.AdminUserDelete(Guid.Parse(Id));
            });
        }
        [HttpPost]
        public JsonResult EditPassWord(string OldPassWord, string PassWord)
        {
            return base.Try(() =>
            {
                if (OperatorProvider.Provider.GetCurrent().PassWord != OldPassWord)
                {
                    throw new  KnownException("旧密码输入错误");
                }
                Guid Id = OperatorProvider.Provider.GetCurrent().ID;
                adminUserApp.EditPassWord(Id, PassWord);
            });
        }
        [HttpPost]
        public JsonResult SaveMenus(string Id,string Menus)
        {
            return base.Try(() =>
            {
                adminUserApp.SaveMenus(Guid.Parse(Id), Menus);
            });
        }
    }
}